package com.example.helloworldspringboot.service;

import com.example.helloworldspringboot.dao.GoodsInterface;
import com.example.helloworldspringboot.entity.Category;
import com.example.helloworldspringboot.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService implements GoodsInterface {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addGoods(Goods goods) {
        return jdbcTemplate.update("insert into goods values(null,?,?,?,?,?,?)",goods.getName(),goods.getPrice(),goods.getQuantity(),goods.getCategory(),goods.getSupplier(),goods.getStatus());
    }

    @Override
    public int deleteGoods(Goods goods) {
        return jdbcTemplate.update("delete from goods where Id=?",goods.getId());
    }

    @Override
    public int updateGoods(Goods goods) {
        return 0;
    }

    @Override
    public Goods queryGoods(Goods goods) {
        return jdbcTemplate.queryForObject("select * from goods where Id="+goods.getId(),new BeanPropertyRowMapper<Goods>(Goods.class));
    }

    @Override
    public List<Goods> queryAllGoods() {
        return jdbcTemplate.query("select * from goods",new BeanPropertyRowMapper<Goods>(Goods.class));
    }
}
